﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

using XMK.Tadong.Model;
using XMK.Tadong.Biz;

namespace XMK.Tadong.ServiceWeb.Handlers
{
    /// <summary>
    /// LogoutAuth 的摘要说明
    /// </summary>
    public class LogoutAuth : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string paraStr = context.Request["data"] ?? "";
            if (paraStr != "" && context.Session!=null&&context.Session["_userid"]!=null)
            {
                if (context.Session["_userid"].ToString() == paraStr)
                {
                    
                    //记录日志
                    XMKLogInfo logModel = new XMKLogInfo();
                    logModel.Keyword = "off";
                    logModel.LogDesc = string.Format("【{0}】【{1}】 Exit system",
                        LoginUserBiz.LoginUserName(), LoginUserBiz.LoginUserAccount());
                    logModel.OperateTime = DateTime.Now;
                    logModel.OperateType = LogOperateType.Logout;
                    logModel.UserId = LoginUserBiz.LoginUserID();
                    logModel.IP = context.Request.ServerVariables["REMOTE_ADDR"].Equals("::1") ? "127.0.0.1" : context.Request.ServerVariables["REMOTE_ADDR"];
                    XMKLogBiz.WriteLog(logModel);

                    context.Session.Clear();
                }
            }
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}